package day10

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

case class Girl(faceVal:Int,age:Int) extends Ordered[Girl]{
  override def compare(that: Girl): Int = {
    if (this.faceVal!=that.faceVal){
      this.faceVal-that.faceVal
    }else{
      that.age - this.age
    }
  }
}

object GirlSort{
  implicit val girlOrdering = new Ordering[Girl]{
    override def compare(x: Girl, y: Girl): Int = {
        if(x.faceVal != y.faceVal) {
          x.faceVal - y.faceVal
        }else{
          y.age - x.age
        }
    }
  }
}


/**
  * 自定义排序
  */
object CustomSort {
  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf().setAppName("CustomSort").setMaster("local[2]")
    val sc: SparkContext = new SparkContext(conf)
    val grilInfo: RDD[(String, Int, Int)] = sc.parallelize(Array(("tingting",80,25),("ningning",100,26),("minmin",100,23)))
    //val res: RDD[(String, Int, Int)] = grilInfo.sortBy(_._2,false)
    //第一种排序
//    import GirlSort.girlOrdering
//    val res: RDD[(String, Int, Int)] = grilInfo.sortBy(x => Girl(x._2,x._3),false)
    //第二种排序
    val res: RDD[(String, Int, Int)] = grilInfo.sortBy(x => Girl(x._2,x._3),false)
    println(res.collect().toBuffer)
    sc.stop()
  }
}
